//
//  ImageDeal.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/18.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "ImageDeal.h"

@implementation ImageDeal

//帖子里只有一张图片时展示的size
CGSize onlyPictureDisplaySize(CGFloat width,CGFloat height) {
    CGSize onlySize = CGSizeZero;
    if (width > height) {//横向
        onlySize.height = kOnlyPicMaxLedgeLength *height/width;
        onlySize.width = kOnlyPicMaxLedgeLength;
    } else {
        onlySize.width = kOnlyPicMaxLedgeLength *height/width;
        onlySize.height =kOnlyPicMaxLedgeLength;
    }
    return onlySize;
}

//将size设置为屏幕宽度,高度等比缩放
CGSize newScreenScaleSize(CGSize size) {
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGSize newSize = CGSizeZero;
    newSize.width = kScreenWidth;
    newSize.height = kScreenWidth/width *height;
    return newSize;
}

//根据设置短边的大小,将size等比例缩放短边的大小]
CGSize newSize(CGSize size,CGFloat minLedge) {
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGSize newSize = size;
    if (height > width) {//高度为长边
        newSize.width = minLedge;
        newSize.height = minLedge * height/width;
    } else {
        newSize.width = minLedge*width/height;
        newSize.height = minLedge ;
    }
    return newSize;
}

CGSize maxSize(CGSize size) {
    CGFloat scale = 2;
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGSize newsize = size;
    if (width/kScreenWidth > scale) {//超过这种精度才需要缩放]
        CGFloat maxWidth = kScreenWidth *scale;
        CGFloat maxHeight = kScreenWidth/width *height *scale;
        newsize.width= maxWidth;
        newsize.height = maxHeight;
    }
    return newsize;
}

//将image尽可能缩放到最大即iPhone6plus的3x，保证在所有手机上都可以正常显示,主要是用来缩放拍照的图片
UIImage *scaleImageToMaxSize(UIImage *image) {
//    kPlusWidth
    CGFloat scale = 3;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    UIImage* scaledImage = image;
    if (width/kPlusWidth > scale) {//超过这种精度才需要缩放]
        CGFloat maxWidth = kPlusWidth * scale;
        CGFloat maxHeight = kPlusWidth/width *height *scale;
        // 创建一个bitmap的context
        // 并把它设置成为当前正在使用的context
        UIGraphicsBeginImageContext(CGSizeMake(maxWidth, maxHeight));
        
        // 绘制改变大小的图片
        [image drawInRect:CGRectMake(0,0, maxWidth, maxHeight)];
        
        // 从当前context中创建一个改变大小后的图片
        scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        
        // 使当前的context出堆栈
        UIGraphicsEndImageContext();
    
    }
    return scaledImage;
}
//size(屏幕上1x的大小)传的是image同比例的大小 ,image传的是原图,结果得到size*scale大小图
UIImage * scaleImageToSize(CGSize size,UIImage *image) {
    if (image ==nil) {
        return nil;
    }
    CGFloat scale = [UIScreen mainScreen].scale;
    UIImage* scaledImage = image;
    if (image.size.width/size.width > scale) {
        // 创建一个bitmap的context
        // 并把它设置成为当前正在使用的context
        UIGraphicsBeginImageContext(CGSizeMake(size.width * scale, size.height *scale));
        
        // 绘制改变大小的图片
        [image drawInRect:CGRectMake(0,0, size.width*scale, size.height*scale)];
        
        // 从当前context中创建一个改变大小后的图片
        scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        
        // 使当前的context出堆栈
        UIGraphicsEndImageContext();
    }
    // 返回新的改变大小后的图片
    return scaledImage;

}
//传入size记得屏幕的1x的size
UIImage * cutCenterImageSize(CGSize size,UIImage *img) {
    CGFloat scale = [UIScreen mainScreen].scale;
    size.width = size.width*scale;
    size.height = size.height *scale;
    CGSize imageSize = img.size;
    CGRect rect;
    //根据图片的大小计算出图片中间矩形区域的位置与大小
    if (imageSize.width > imageSize.height) {
        float leftMargin = (imageSize.width - imageSize.height) *0.5;
        rect = CGRectMake(leftMargin,0, imageSize.height, imageSize.height);
    }else{
        float topMargin = (imageSize.height - imageSize.width) *0.5;
        rect = CGRectMake(0, topMargin, imageSize.width, imageSize.width);
    }
    
    CGImageRef imageRef = img.CGImage;
    //截取中间区域矩形图片
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    
    UIImage *tmp = [[UIImage alloc] initWithCGImage:imageRefRect];
    CGImageRelease(imageRefRect);
    
    UIGraphicsBeginImageContext(size);
    CGRect rectDraw =CGRectMake(0,0, size.width, size.height);
    [tmp drawInRect:rectDraw];
    // 从当前context中创建一个改变大小后的图片
    tmp = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
//    NSLog(@"tmp sizewidth is %f sizeHeight is %f",tmp.size.width,tmp.size.height);
    return tmp;
}

UIImage * coop_cachedImageWithURL(NSURL *url) {
    if([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:[url absoluteString]]) {
        return [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:[url absoluteString]];
    }else if ([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[url absoluteString]]) {
        return [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[url absoluteString]];
    }else {
        return coop_setImageURL(url);
    }
}
UIImage * coop_setImageURL(NSURL *url) {
    UIImageView *imageViwe = [[UIImageView alloc]init];
    
    [imageViwe sd_setImageWithURL:url];
    
    return imageViwe.image;
}


@end
